package com.mouse.workflow.activiti.services;

import com.mouse.workflow.activiti.user.dao.PersonRepository;
import com.mouse.workflow.activiti.user.entity.Person;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cwx183898 on 2017/8/2.
 */
@Component
public class MyService {


    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;


    @Autowired
    private PersonRepository personRepository;

    public void startProcess(String assignee) {
        Person person = personRepository.findByUsername(assignee);

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("person", person);
        variables.put("orderId", "testId");
        runtimeService.startProcessInstanceByKey("oneTaskProcess", variables);
    }

    public List<Task> getTasks(String assignee) {
        return taskService.createTaskQuery().taskAssignee(assignee).list();
    }

    public void createDemoUsers() {
        Person person = personRepository.findByUsername("cwx183898");
        if (person == null) {
            personRepository.save(new Person("cwx183898", "chen", "jiabin", new Date()));
        }
    }

}
